package image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgPainter {
	public BufferedImage img;
	public Point originalSize;

	public ImgPainter(String fn) throws IOException {
		originalSize = new Point();
		img = loadImage(fn);
	}

	public void paint(Graphics g, Color color, int ox, int oy, int sx, int sy) {
		if (null != img)
			g.drawImage(img, ox, oy, sx, sy, null);
		else {
			if (null != color)
				g.setColor(color);
			g.drawRect(ox, oy, sx - 1, sy - 1);
		}
	}

	BufferedImage loadImage(String fn) throws IOException {
		BufferedImage img = null;
		File f = new File(fn);
		img = mkImageTransparent(ImageIO.read(f));
		originalSize.x = img.getWidth();
		originalSize.y = img.getHeight();
		return img;
	}

	public static BufferedImage mkImageTransparent(BufferedImage imgx) {
		BufferedImage img = null;
		int c = imgx.getRGB(0, 0);

		// http://www.javalobby.org/articles/ultimate-image/
		img = new BufferedImage(imgx.getWidth(), imgx.getHeight(),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = img.createGraphics();
		g.setComposite(AlphaComposite.Src);
		g.drawImage(imgx, null, 0, 0);
		g.dispose();
		for (int i = 0; i < img.getHeight(); i++) {
			for (int j = 0; j < img.getWidth(); j++) {
				boolean t = c == img.getRGB(j, i);
				if (j > 1)
					t |= c == img.getRGB(j - 1, i);
				if (i > 1)
					t |= c == img.getRGB(j, i - 1);
				if (j > 1 && i > 1)
					t |= c == img.getRGB(j - 1, i - 1);

				if (t) {
					img.setRGB(j, i, 0x8F1C1C);
				}
			}
		}
		return img;
	}

	public static BufferedImage imageStretch(BufferedImage src, int dx, int dy) {
		BufferedImage dst = null;
		// http://www.javalobby.org/articles/ultimate-image/
		dst = new BufferedImage(dx, dy, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = dst.createGraphics();
		g.setComposite(AlphaComposite.Src);
		int sy = src.getHeight();
		for (int i = 0; i < dy; i++) {
			int y = (i * sy) / dy;
			int sx = src.getWidth();
			for (int j = 0; j < dx; j++) {
				int x = (j * sx) / dx;
				dst.setRGB(j, i, src.getRGB(x, y));
			}
		}
		g.dispose();
		return dst;
	}

	public static double getColorDistance(Color color1, Color color2) {
		double r = color1.getRed() - color2.getRed();
		double g = color1.getGreen() - color2.getGreen();
		double b = color1.getBlue() - color2.getBlue();
		r = r * r;
		g = g * g;
		b = b * b;
		return Math.sqrt(r + g + b);

	}

	public static Point findColoredPoint(BufferedImage src, Color color,
			double howFar) {
		int dy = src.getHeight();
		int dx = src.getWidth();
		Point point = null;
		double distance = howFar + 2000;
		for (int y = 0; y < dy; y++) {
			for (int x = 0; x < dx; x++) {
				double d = getColorDistance(color, new Color(src.getRGB(x, y)));
				if (d < distance) {
					point = new Point(x, y);
					distance = d;
				}
			}
		}
		return point;
	}

	public Point Dupa(Dimension dimension, Point point) {
		if (null != originalSize && originalSize.x * originalSize.y != 0) {
			int x = (point.x * dimension.width) / originalSize.x;
			int y = (point.y * dimension.height) / originalSize.y;
			return new Point(x, y);
		}
		return null;
	}

	public static void makeGrid(Graphics src, Dimension dim, int i, int j,
			Matrix matrix) {

		int dy = dim.height / j;
		int dx = dim.width;
		Graphics g = src;
		g.setColor(Color.blue);
		for (int y = 0; y < j; y++) {
			g.drawLine(0, y * dy, dx, y * dy);
		}

		dy = dim.height;
		dx = dim.width / i;
		g.setColor(Color.blue);
		for (int x = 0; x < i; x++) {
			g.drawLine(x * dx, 0, x * dx, dy / 2);
		}
		if (null != matrix)
			for (int x = 0; x < i; x++) {
				for (int y = 0; y < j; y++) {
				}
			}
	}

	public Matrix createMatrix(int i, int j) {
		Matrix m = new Matrix(i, j);
		Dimension dim = new Dimension(img.getWidth(), img.getHeight());
		int dy = dim.height / j;
		int dx = dim.width / i;
		Graphics g = img.getGraphics();
		g.setColor(Color.blue);
		for (int x = 0; x < i; x++) {
			for (int y = 0; y < j; y++) {
				BufferedImage subImage = img.getSubimage(x, y, dx, dy);
				m.setValue(x, y, analyze(subImage, dx, dy));
			}
		}
		return m;
	}

	private int analyze(BufferedImage subImage, int dx, int dy) {
		if (null == ImgPainter.findColoredPoint(subImage, Color.black, 1000))
			return 0;
		else
			return -100;
	}
}
